1506D - Epic Transformation - CodeForces Solution


constructive algorithms data structures greedy *1400

Please click on ads to support us..

Python Code:

from heapq import heappop, heappush
import sys
lines = list(map(str.strip, sys.stdin.readlines()))

for line in lines[2::2]:
    nums = list(map(int, line.split(" ")))
    counts = {}
    for x in nums:
        counts[x] = 1 if x not in counts else counts[x] + 1
    count_key = [(counts[key], key) for key in counts]
    hq = []
    for count, key in count_key:
        heappush(hq, -count)
    while len(hq) > 1:
        a = -heappop(hq)
        b = -heappop(hq)
        a -= 1
        b -= 1
        if a: heappush(hq, -a)
        if b: heappush(hq, -b)
    print(0 if not hq else -hq[0])

C++ Code:

/* ____                       _  __                     _       _       _       
  |  _ \ _ __ ___ _ __ ___   | |/ /__ _ _ __ ___   __ _| |     | | __ _(_)_ __  
  | |_) | '__/ _ \ '_ ` _ \  | ' // _` | '_ ` _ \ / _` | |  _  | |/ _` | | '_ \ 
  |  __/| | |  __/ | | | | | | . \ (_| | | | | | | (_| | | | |_| | (_| | | | | |
  |_|   |_|  \___|_| |_| |_| |_|\_\__,_|_| |_| |_|\__,_|_|  \___/ \__,_|_|_| |_|
*/                                                                            

#include<bits/stdc++.h>
using namespace std;

typedef long long int ll;
typedef unsigned long long ull;
typedef long double lld;

#define hell    1000000007
#define PI      3.141592653589793238462
#define INF     1e18
#define vi      vector<ll>
#define pb      push_back
#define ppb     pop_back
#define mii     map<ll,ll>
#define pii     pair<ll,ll>
#define mp      make_pair
#define ff      first
#define ss      second
#define all(x)  (x).begin(), (x).end()

/*____________________________________________________________________________________________________________________________________________________________________________________________________*/
vector<ll> sieve(int n) {int*arr = new int[n + 1](); vector<ll> vect; for (int i = 2; i <= n; i++)if (arr[i] == 0) {vect.push_back(i); for (int j = 2 * i; j <= n; j += i)arr[j] = 1;} return vect;}
ll mod_add(ll a, ll b, ll m) {a = a % m; b = b % m; return (((a + b) % m) + m) % m;}
ll mod_mul(ll a, ll b, ll m) {a = a % m; b = b % m; return (((a * b) % m) + m) % m;}
ll mod_sub(ll a, ll b, ll m) {a = a % m; b = b % m; return (((a - b) % m) + m) % m;}
ll expo(ll a, ll b, ll mod) {ll res = 1; while (b > 0) {if (b & 1)res = (res * a) % mod; a = (a * a) % mod; b = b >> 1;} return res;}
/*____________________________________________________________________________________________________________________________________________________________________________________________________*/


void solve()
{
    ll n;
    cin>>n;
    mii ma;
    for(ll i=0;i<n;i++){
        ll x;
        cin>>x;
        ma[x]++;
    }
    priority_queue<ll> pq;
    for(auto it : ma){
        pq.push(it.ss);
    }
    
    
    while(pq.size()>1){
        ll x=pq.top();
        pq.pop();
        ll y=pq.top();
        pq.pop();
        if(x>1){pq.push(x-1);}
        if(y>1){pq.push(y-1);}
    }
    pq.push(0);
    cout<<pq.top()<<endl;
    return;
}

int main()
{
    ios_base::sync_with_stdio(false);
    int t=1;
    cin>>t;
    for(ll i=0;i<t;i++){
        solve();
    }
    return 0;
}


Comments

Submit
0 Comments
More Questions

1384A - Common Prefixes
371A - K-Periodic Array
1542A - Odd Set
1567B - MEXor Mixup
669A - Little Artem and Presents
691B - s-palindrome
851A - Arpa and a research in Mexican wave
811A - Vladik and Courtesy
1006B - Polycarp's Practice
1422A - Fence
21D - Traveling Graph
1559B - Mocha and Red and Blue
1579C - Ticks
268B - Buttons
898A - Rounding
1372B - Omkar and Last Class of Math
1025D - Recovering BST
439A - Devu the Singer and Churu the Joker
1323A - Even Subset Sum Problem
1095A - Repeating Cipher
630F - Selection of Personnel
630K - Indivisibility
20B - Equation
600B - Queries about less or equal elements
1015A - Points in Segments
1593B - Make it Divisible by 25
680C - Bear and Prime 100
1300A - Non-zero
1475E - Advertising Agency
1345B - Card Constructions